package com.huiren.ui.configUI;

import javax.annotation.PostConstruct;
import javax.swing.JPanel;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.huiren.util.JdbcUtil;
import com.huiren.util.XMLUtil;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class GroupParamJPanel extends JPanel {

	private static Logger logger = Logger.getLogger(GroupParamJPanel.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public GroupParamJPanel() {

	}

	@PostConstruct
	public void initMethod() {
		setLayout(null);
		getColumns();
		addButton();
	}

	// 获取所有被选中的jcheckbox
	public List<JCheckBox> getSelectedBox() {
		List<JCheckBox> list = new ArrayList<JCheckBox>();
		java.awt.Component[] comps = this.getComponents();
		if (comps.length > 0) {
			for (int i = 0; i < comps.length; i++) {
				java.awt.Component comp = comps[i];
				if (comp instanceof JCheckBox) {
					JCheckBox box = (JCheckBox) comp;
					if (box.isSelected()) {
						list.add(box);
					}
				}
			}
		}
		return list;
	}

	// 添加button
	private void addButton() {
		JButton resetButton = new JButton("重置");
		resetButton.setBounds(155, 496, 93, 23);
		add(resetButton);
		resetButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getColumns();
				addButton();
				updateUI();
			}
		});

		JButton saveButton = new JButton("保存");
		saveButton.setBounds(427, 496, 93, 23);
		add(saveButton);
		saveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int type = JOptionPane.showConfirmDialog(null, "您确定要保存吗?",
						"提示", JOptionPane.YES_NO_OPTION,
						JOptionPane.QUESTION_MESSAGE);
				if (type == JOptionPane.YES_NO_OPTION) {
					List<JCheckBox> checkboxs = getSelectedBox();
					StringBuffer groups = new StringBuffer();
					for (int i = 0; i < checkboxs.size(); i++) {
						JCheckBox checkbox = checkboxs.get(i);
						if (i == 0) {
							groups.append(checkbox.getName());
						} else {
							groups.append(",");
							groups.append(checkbox.getName());
						}
					}

					JdbcUtil.executeUpdate("update T_YJ_CONFIG set value='"
							+ groups + "' where key='group'");
					logger.info("保存数据链接");
				}

			}
		});
	}

	// 获取字段并生成checkbox
	private void getColumns() {
		this.removeAll();
		XMLUtil xmlUtil = new XMLUtil();
		List<String> sql = xmlUtil.selectNodes("/sql/" + JdbcUtil.getJdbcType()
				+ "/selectcolumns");
		List<Map<String, Object>> columns = JdbcUtil.executeQuery(sql.get(0));
		List<Map<String, Object>> groupResult = JdbcUtil
				.executeQuery("select * from T_YJ_CONFIG where key ='group'");
		if (groupResult.size() != 0 && columns.size() != 0) {
			String groupStr = String.valueOf(groupResult.get(0).get("value"))
					.toLowerCase();
			String[] groups = groupStr.split(",");

			int h = 0;
			int w = 100;

			for (int i = 0; i < columns.size(); i++) {
				Map<String, Object> map = columns.get(i);
				if (i % 2 == 0) {
					h = h + 40;
					w = 100;
				} else {
					w = w + 300;
				}
				String column_name = String.valueOf(map.get("column_name"))
						.toLowerCase();
				String comments = String.valueOf(map.get("comments"));
				JLabel lblNewLabel_1 = new JLabel(column_name + " " + comments);
				lblNewLabel_1.setBounds(w, h, 200, 15);
				add(lblNewLabel_1);
				JCheckBox jcheckbox = new JCheckBox();
				jcheckbox.setName(column_name);
				for (String group : groups) {
					if (group.equals(column_name)) {
						jcheckbox.setSelected(true);
						break;
					}
				}
				jcheckbox.setBounds(w + 200, h, 30, 21);
				add(jcheckbox);
			}
		}
	}
}
